<?php

class Zhishidi_Question_IndexController extends Mage_Core_Controller_Front_Action {
    const XML_DAILY_DEALS_SETTINGS_PATH = "zhishidi_question_info/settings/";

    public function indexAction() {

          
        $this->loadLayout();
        $this->getLayout()->getBlock('head')->setTitle($this->__(Mage::getStoreConfig(self::XML_DAILY_DEALS_SETTINGS_PATH . "meta_title")));
        $this->getLayout()->getBlock('head')->setDescription($this->__(Mage::getStoreConfig(self::XML_DAILY_DEALS_SETTINGS_PATH . "meta_description")));
        $this->getLayout()->getBlock('head')->setKeywords($this->__(Mage::getStoreConfig(self::XML_DAILY_DEALS_SETTINGS_PATH . "meta_keywords")));
        $this->getLayout()->getBlock('head')->setRobots($this->__(Mage::getStoreConfig(self::XML_DAILY_DEALS_SETTINGS_PATH . "settings/robots")));

        $this->renderLayout();
    }

    public function askAction() {
        $post = $this->getRequest()->getPost();
        if ($post) {
            try {
                $postObject = new Varien_Object();
                $postObject->setData($post);

                $error = false;

                if (!Zend_Validate::is(trim($post['name']), 'NotEmpty')) {
                    $error = true;
                }

                if (!Zend_Validate::is(trim($post['question']), 'NotEmpty')) {
                    $error = true;
                }

                if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                    $error = true;
                }

                if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                    $error = true;
                }

                if ($error) {
                    throw new Exception();
                }

                $post['ip'] = Mage::helper('core/http')->getRemoteAddr();

                $post['created_on']= date('Y-m-d H:i:s', Mage::app()->getLocale()->storeTimeStamp());

                $model = Mage::getModel('zhishidi_question/question');
                $model->setData($post);
                $model->save();


                Mage::getSingleton('catalog/session')->addSuccess(Mage::helper('zhishidi_question')->__('Thank You! We\'ll reply to your question as soon as possible '));

                return $this->_redirectReferer();
            } catch (Exception $e) {
                Mage::getSingleton('catalog/session')->addError(Mage::helper('zhishidi_question')->__('Unable to submit your question. Please, try again later' . $e->getMessage()));
                return $this->_redirectReferer();
            }
        } else {
            return $this->_redirectReferer();
        }
    }

}

?>
